package com.news.controller;

import com.news.model.Category;
import com.news.model.News;
import com.news.service.CategoryService;
import com.news.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;
    @Autowired
    private CategoryService categoryService;
    @RequestMapping("/index")
    public String queryIndex(Model model){
        List<Category> query = categoryService.query();
        List<News> news = new ArrayList<>();
        for(Category category : query){
           List<News> list =  newsService.queryFiveNews(category.getId());
            news.addAll(list);

        }
        model.addAttribute("list",news);
        //buchongzhaopianxinxi
        System.out.println(news);
        return "index";
    }
    @RequestMapping("/category/{pageNum}/{pageSize}")
    public String queryCategory(String id, Model model, @PathVariable("pageNum")int pageNum, @PathVariable("pageSize") int pageSize){
        List<News> news = newsService.queryCategory(id,pageNum,pageSize);
        List<News> news1 = newsService.queryAll(id);
        model.addAttribute("pageNum",pageNum);
        int pageCount = 0;
        if(news1.size() % pageSize == 0){
            pageCount = news1.size() / pageSize;
        }else{
            pageCount = news1.size() / pageSize + 1;
        }
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("news",news);
        return "features-columns";
    }
    @RequestMapping("/queryLike")
    public String queryLike(String str, Model model){
        List<News> news = newsService.queryLike(str);
        model.addAttribute("news",news);
        return "features-columns";
    }
}
